package com.lg.wxq.ui;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.lg.wxq.adapter.MainTabAdapter;
import com.lg.wxq.manager.MainActivityManager;
import com.lg.wxq.utils.DebugUtils;

public class MainActivity extends TabActivity {

	private TabHost th;
	private LinearLayout itemLl;
	private ListView itemList;
	private LinearLayout ll;
	private ImageView iv;
	private TranslateAnimation animation;
	private FrameLayout fl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintab);
		MainActivityManager.getInstance().setMainActivity(this);

		itemLl = (LinearLayout) findViewById(R.id.maintab_ll_below);
		iv = (ImageView) findViewById(R.id.maintab_iv);
		fl = (FrameLayout) findViewById(android.R.id.tabcontent);
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		layoutParams.rightMargin = 81;
		LayoutInflater inflater = LayoutInflater.from(this);
		RelativeLayout below = (RelativeLayout) inflater.inflate(R.layout.below, null);
		itemLl.addView(below, layoutParams);
		itemList = (ListView) itemLl.findViewById(R.id.maintab_lv);
		itemList.setVerticalScrollBarEnabled(false);
		itemList.setHorizontalScrollBarEnabled(false);
		itemList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				// th.setCurrentTabByTag("TS_MESSAGE");
				((MainTabAdapter) itemList.getAdapter()).changeBackGround(arg2);
			}
		});
		MainTabAdapter adapter = new MainTabAdapter(this);
		DebugUtils.isNull("itemList", itemList);
		itemList.setAdapter(adapter);

		th = this.getTabHost();
		// 1、添加home分页
		TabSpec tsHome = th.newTabSpec("TS_HOME").setIndicator("TS_HOME").setContent(new Intent(this, HomeActivity.class));
		th.addTab(tsHome);

		// 2、添加message分页
		TabSpec tsMessage = th.newTabSpec("TS_MESSAGE").setIndicator("TS_MESSAGE").setContent(new Intent(this, MessageActivity.class));
		th.addTab(tsMessage);

		th.setCurrentTabByTag("TS_HOME");
	}

	public void clickButton(View v) {
		fl.setVisibility(View.VISIBLE);
		animation = new TranslateAnimation(Animation.ABSOLUTE, 400.0f, Animation.ABSOLUTE, 0f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f);
		animation.setAnimationListener(new AnimationListener() {

			public void onAnimationStart(Animation animation) {
			}

			public void onAnimationRepeat(Animation animation) {
			}

			public void onAnimationEnd(Animation animation) {
				itemVisiable(false);
			}
		});
		animation.setDuration(300);
		fl.startAnimation(animation);
	}

	public void itemVisiable(boolean isShow) {
		itemLl.setVisibility(isShow ? View.VISIBLE : View.GONE);
		if (isShow) {
			take();
			animation = new TranslateAnimation(Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 400f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f);
			animation.setAnimationListener(new AnimationListener() {

				public void onAnimationStart(Animation animation) {
				}

				public void onAnimationRepeat(Animation animation) {
				}

				public void onAnimationEnd(Animation animation) {

					fl.setVisibility(View.GONE);
				}
			});
			animation.setDuration(300);
			fl.startAnimation(animation);
		}
	}

	private void take() {
		Bitmap takeScreenShot = takeScreenShot(this);
		iv.setImageBitmap(takeScreenShot);
	}

	// 获取指定Activity的截屏，保存到png文件
	private static Bitmap takeScreenShot(Activity activity) {
		// View是你需要截图的View
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap b1 = view.getDrawingCache();

		// 获取状态栏高度
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;
		Log.i("TAG", "" + statusBarHeight);

		// 获取屏幕长和高
		int width = activity.getWindowManager().getDefaultDisplay().getWidth();
		int height = activity.getWindowManager().getDefaultDisplay().getHeight();
		// 去掉标题栏
		// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
		Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, 81, height - statusBarHeight);
		view.destroyDrawingCache();
		return b;
	}

}
